package br.univas.vperiod.jowey.dao;

import java.util.ArrayList;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import br.univas.vperiod.jowey.entities.PlayerJob;
import br.univas.vperiod.jowey.entities.Project;

@Repository
@Transactional
public class ProjectDAO implements IProjectDAO<Project> {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Autowired
	private SessionFactory factory;

	public void save(Project project) {
		factory.getCurrentSession().save(project);
	}

	@SuppressWarnings("unchecked")
	public ArrayList<Project> findAll() {
		return (ArrayList<Project>) factory.getCurrentSession().createCriteria(Project.class).list();
	}

	public Project findByName(String name) {
		return (Project) factory.getCurrentSession().createCriteria(Project.class).uniqueResult();
	}

	@SuppressWarnings("unchecked")
	public ArrayList<PlayerJob> getPlayersInProjects(int idPlayer){
		return (ArrayList<PlayerJob>) factory.getCurrentSession()
				.createCriteria(PlayerJob.class)
				.add(Restrictions.eq("idplayer",idPlayer).ignoreCase()).list();
	}
}